<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- dynamicview-tutorial.qdoc -->
  <title>QML Dynamic View Ordering Tutorial 4 - Sorting Items | Qt Quick 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtquick-index.html">Qt Quick 模块</a></td><td >
			QML 动态视图排序教程 4 - 排序项</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="qtquick-tutorials-dynamicview-dynamicview3-example.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-dynamicview-dynamicview3-example.html">QML 动态视图排序教程 3 - 移动拖动项</a>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level2"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-dynamicview-dynamicview4-example.html#walkthrough">演练</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QML 动态视图排序教程 4 - 排序项</h1>
<span class="subtitle"></span>
<!-- $$$tutorials/dynamicview/dynamicview4-description -->
<div class="descr"> <a name="details"></a>
<p>拖放并不是对视图中的项目进行重新排序的唯一方式，使用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qml-qtqml-models-delegatemodel.html">DelegateModel</a>还可以根据模型数据对项目进行排序。为此，我们像这样扩展我们的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qml-qtqml-models-delegatemodel.html">DelegateModel</a>实例：</p>
<pre class="qml">

      <span class="type"><a href="../qtqml/qml-qtqml-models-delegatemodel.html">DelegateModel</a></span> {
          <span class="name">id</span>: <span class="name">visualModel</span>
          property <span class="type"><a href="../qtqml/qml-var.html">var</a></span> <span class="name">lessThan</span>: [
              <span class="keyword">function</span>(left, right) { <span class="keyword">return</span> <span class="name">left</span>.<span class="name">name</span> <span class="operator">&lt;</span> <span class="name">right</span>.<span class="name">name</span> },
              <span class="keyword">function</span>(left, right) { <span class="keyword">return</span> <span class="name">left</span>.<span class="name">type</span> <span class="operator">&lt;</span> <span class="name">right</span>.<span class="name">type</span> },
              <span class="keyword">function</span>(left, right) { <span class="keyword">return</span> <span class="name">left</span>.<span class="name">age</span> <span class="operator">&lt;</span> <span class="name">right</span>.<span class="name">age</span> },
              <span class="keyword">function</span>(left, right) {
                  <span class="keyword">if</span> (<span class="name">left</span>.<span class="name">size</span> <span class="operator">==</span> <span class="string">&quot;Small&quot;</span>)
                      <span class="keyword">return</span> <span class="number">true</span>
                  <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">right</span>.<span class="name">size</span> <span class="operator">==</span> <span class="string">&quot;Small&quot;</span>)
                      <span class="keyword">return</span> <span class="number">false</span>
                  <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">left</span>.<span class="name">size</span> <span class="operator">==</span> <span class="string">&quot;Medium&quot;</span>)
                      <span class="keyword">return</span> <span class="number">true</span>
                  <span class="keyword">else</span>
                      <span class="keyword">return</span> <span class="number">false</span>
              }
          ]

          property <span class="type"><a href="../qtqml/qml-int.html">int</a></span> <span class="name">sortOrder</span>: <span class="name">orderSelector</span>.<span class="name">selectedIndex</span>
          <span class="name">onSortOrderChanged</span>: <span class="name">items</span>.<span class="name">setGroups</span>(<span class="number">0</span>, <span class="name">items</span>.<span class="name">count</span>, <span class="string">&quot;unsorted&quot;</span>)

          <span class="keyword">function</span> <span class="name">insertPosition</span>(lessThan, item) {
              var lower = <span class="number">0</span>
              var upper = <span class="name">items</span>.<span class="name">count</span>
              <span class="keyword">while</span> (<span class="name">lower</span> <span class="operator">&lt;</span> <span class="name">upper</span>) {
                  var middle = <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">lower</span> <span class="operator">+</span> (<span class="name">upper</span> <span class="operator">-</span> <span class="name">lower</span>) <span class="operator">/</span> <span class="number">2</span>)
                  var result = <span class="name">lessThan</span>(<span class="name">item</span>.<span class="name">model</span>, <span class="name">items</span>.<span class="name">get</span>(<span class="name">middle</span>).<span class="name">model</span>);
                  <span class="keyword">if</span> (<span class="name">result</span>) {
                      <span class="name">upper</span> <span class="operator">=</span> <span class="name">middle</span>
                  } <span class="keyword">else</span> {
                      <span class="name">lower</span> <span class="operator">=</span> <span class="name">middle</span> <span class="operator">+</span> <span class="number">1</span>
                  }
              }
              <span class="keyword">return</span> <span class="name">lower</span>
          }

          <span class="keyword">function</span> <span class="name">sort</span>(lessThan) {
              <span class="keyword">while</span> (<span class="name">unsortedItems</span>.<span class="name">count</span> <span class="operator">&gt;</span> <span class="number">0</span>) {
                  var item = <span class="name">unsortedItems</span>.<span class="name">get</span>(<span class="number">0</span>)
                  var index = <span class="name">insertPosition</span>(<span class="name">lessThan</span>, <span class="name">item</span>)

                  <span class="name">item</span>.<span class="name">groups</span> <span class="operator">=</span> <span class="string">&quot;items&quot;</span>
                  <span class="name">items</span>.<span class="name">move</span>(<span class="name">item</span>.<span class="name">itemsIndex</span>, <span class="name">index</span>)
              }
          }

          <span class="name">items</span>.includeByDefault: <span class="number">false</span>
          <span class="name">groups</span>: <span class="name">DelegateModelGroup</span> {
              <span class="name">id</span>: <span class="name">unsortedItems</span>
              <span class="name">name</span>: <span class="string">&quot;unsorted&quot;</span>

              <span class="name">includeByDefault</span>: <span class="number">true</span>
              <span class="name">onChanged</span>: {
                  <span class="keyword">if</span> (<span class="name">visualModel</span>.<span class="name">sortOrder</span> <span class="operator">==</span> <span class="name">visualModel</span>.<span class="name">lessThan</span>.<span class="name">length</span>)
                      <span class="name">setGroups</span>(<span class="number">0</span>, <span class="name">count</span>, <span class="string">&quot;items&quot;</span>)
                  <span class="keyword">else</span>
                      <span class="name">visualModel</span>.<span class="name">sort</span>(<span class="name">visualModel</span>.<span class="name">lessThan</span>[<span class="name">visualModel</span>.<span class="name">sortOrder</span>])
              }
          }
          <span class="name">model</span>: <span class="name">PetsModel</span> {}
          <span class="name">delegate</span>: <span class="name">dragDelegate</span>
      }

</pre>
<a name="walkthrough"></a>
<h3 >演练</h3>
<p><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qml-qtqml-models-delegatemodel.html">DelegateModel</a>中的项目被过滤到由<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qml-qtqml-models-delegatemodelgroup.html">DelegateModelGroup</a>类型表示的组中，通常模型中的所有项目都属于默认<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qml-qtqml-models-delegatemodel.html#items-prop">项</a>组，但可以使用 includeByDefault 属性更改此默认值。为了实现我们的排序，我们希望首先将项目添加到未排序的组中，然后我们可以将它们转移到项目组中的排序位置。为此，我们清除项目组上的 includeByDefault 并将其设置为新的组名“未排序”。</p>
<pre class="qml">

          <span class="name">items</span>.includeByDefault: <span class="number">false</span>
          <span class="name">groups</span>: <span class="name">DelegateModelGroup</span> {
              <span class="name">id</span>: <span class="name">unsortedItems</span>
              <span class="name">name</span>: <span class="string">&quot;unsorted&quot;</span>

              <span class="name">includeByDefault</span>: <span class="number">true</span>
          }

</pre>
<p>我们通过首先在项目组中找到插入第一个未排序项目的位置来对项目进行排序，然后将项目转移到项目组，然后将其移动到预定索引并重复直到未排序组为空。</p>
<p>为了找到一个项的插入位置，我们使用<a href="../qtqml/qml-qtqml-models-delegatemodelgroup.html#get-method">get()</a>函数从未排序的组中请求该项的句柄。通过这个句柄上的model属性，我们可以访问该项的委托实例中可用的相同模型数据，并与其他项进行比较，以确定相对位置。</p>
<pre class="qml">

          <span class="keyword">function</span> <span class="name">insertPosition</span>(lessThan, item) {
              var lower = <span class="number">0</span>
              var upper = <span class="name">items</span>.<span class="name">count</span>
              <span class="keyword">while</span> (<span class="name">lower</span> <span class="operator">&lt;</span> <span class="name">upper</span>) {
                  var middle = <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">lower</span> <span class="operator">+</span> (<span class="name">upper</span> <span class="operator">-</span> <span class="name">lower</span>) <span class="operator">/</span> <span class="number">2</span>)
                  var result = <span class="name">lessThan</span>(<span class="name">item</span>.<span class="name">model</span>, <span class="name">items</span>.<span class="name">get</span>(<span class="name">middle</span>).<span class="name">model</span>);
                  <span class="keyword">if</span> (<span class="name">result</span>) {
                      <span class="name">upper</span> <span class="operator">=</span> <span class="name">middle</span>
                  } <span class="keyword">else</span> {
                      <span class="name">lower</span> <span class="operator">=</span> <span class="name">middle</span> <span class="operator">+</span> <span class="number">1</span>
                  }
              }
              <span class="keyword">return</span> <span class="name">lower</span>
          }

          <span class="keyword">function</span> <span class="name">sort</span>(lessThan) {
              <span class="keyword">while</span> (<span class="name">unsortedItems</span>.<span class="name">count</span> <span class="operator">&gt;</span> <span class="number">0</span>) {
                  var item = <span class="name">unsortedItems</span>.<span class="name">get</span>(<span class="number">0</span>)
                  var index = <span class="name">insertPosition</span>(<span class="name">lessThan</span>, <span class="name">item</span>)

                  <span class="name">item</span>.<span class="name">groups</span> <span class="operator">=</span> <span class="string">&quot;items&quot;</span>
                  <span class="name">items</span>.<span class="name">move</span>(<span class="name">item</span>.<span class="name">itemsIndex</span>, <span class="name">index</span>)
              }
          }

</pre>
<p>sort 函数的 lessThan 参数是一个比较函数，它将确定列表的顺序。在此示例中，它可以是以下之一：</p>
<pre class="qml">

          property <span class="type"><a href="../qtqml/qml-var.html">var</a></span> <span class="name">lessThan</span>: [
              <span class="keyword">function</span>(left, right) { <span class="keyword">return</span> <span class="name">left</span>.<span class="name">name</span> <span class="operator">&lt;</span> <span class="name">right</span>.<span class="name">name</span> },
              <span class="keyword">function</span>(left, right) { <span class="keyword">return</span> <span class="name">left</span>.<span class="name">type</span> <span class="operator">&lt;</span> <span class="name">right</span>.<span class="name">type</span> },
              <span class="keyword">function</span>(left, right) { <span class="keyword">return</span> <span class="name">left</span>.<span class="name">age</span> <span class="operator">&lt;</span> <span class="name">right</span>.<span class="name">age</span> },
              <span class="keyword">function</span>(left, right) {
                  <span class="keyword">if</span> (<span class="name">left</span>.<span class="name">size</span> <span class="operator">==</span> <span class="string">&quot;Small&quot;</span>)
                      <span class="keyword">return</span> <span class="number">true</span>
                  <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">right</span>.<span class="name">size</span> <span class="operator">==</span> <span class="string">&quot;Small&quot;</span>)
                      <span class="keyword">return</span> <span class="number">false</span>
                  <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">left</span>.<span class="name">size</span> <span class="operator">==</span> <span class="string">&quot;Medium&quot;</span>)
                      <span class="keyword">return</span> <span class="number">true</span>
                  <span class="keyword">else</span>
                      <span class="keyword">return</span> <span class="number">false</span>
              }
          ]

</pre>
<p>每当将新项目添加到未排序的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qml-qtqml-models-delegatemodel.html">DelegateModel</a>时，都会触发排序，我们会收到<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qml-qtqml-models-delegatemodelgroup.html">DelegateModelGroup</a> <code>onChanged</code>处理程序的通知。如果当前没有选择排序函数，我们只需将所有项目从未排序组转移到项目组，否则我们使用选定的排序函数调用排序。</p>
<pre class="qml">

          <span class="name">groups</span>: <span class="name">DelegateModelGroup</span> {
              <span class="name">id</span>: <span class="name">unsortedItems</span>
              <span class="name">name</span>: <span class="string">&quot;unsorted&quot;</span>

              <span class="name">includeByDefault</span>: <span class="number">true</span>
              <span class="name">onChanged</span>: {
                  <span class="keyword">if</span> (<span class="name">visualModel</span>.<span class="name">sortOrder</span> <span class="operator">==</span> <span class="name">visualModel</span>.<span class="name">lessThan</span>.<span class="name">length</span>)
                      <span class="name">setGroups</span>(<span class="number">0</span>, <span class="name">count</span>, <span class="string">&quot;items&quot;</span>)
                  <span class="keyword">else</span>
                      <span class="name">visualModel</span>.<span class="name">sort</span>(<span class="name">visualModel</span>.<span class="name">lessThan</span>[<span class="name">visualModel</span>.<span class="name">sortOrder</span>])
              }
          }

</pre>
<p>最后，当选定的排序顺序发生变化时，我们可以通过将所有项目从项目组移动到未排序的组来触发对列表的完整重新排序，这将触发<a href="../qtqml/qml-qtqml-models-delegatemodelgroup.html">DelegateModelGroup</a> <code>onChanged</code>处理程序，并以正确的顺序将项目转移回项目组。请注意，<a href="../qtqml/qml-qtqml-models-delegatemodelgroup.html">DelegateModelGroup</a> <code>onChanged</code>处理程序不会被递归调用，因此在排序期间调用它没有问题。</p>
<pre class="qml">

          property <span class="type"><a href="../qtqml/qml-int.html">int</a></span> <span class="name">sortOrder</span>: <span class="name">orderSelector</span>.<span class="name">selectedIndex</span>
          <span class="name">onSortOrderChanged</span>: <span class="name">items</span>.<span class="name">setGroups</span>(<span class="number">0</span>, <span class="name">items</span>.<span class="name">count</span>, <span class="string">&quot;unsorted&quot;</span>)

</pre>
<p>文件：</p>
<ul>
<li><a href="qtquick-tutorials-dynamicview-dynamicview4-listselector-qml.html">tutorials/dynamicview/dynamicview4/ListSelector.qml</a></li>
<li><a href="qtquick-tutorials-dynamicview-dynamicview4-petsmodel-qml.html">tutorials/dynamicview/dynamicview4/PetsModel.qml</a></li>
<li><a href="qtquick-tutorials-dynamicview-dynamicview4-dynamicview-qml.html">tutorials/dynamicview/dynamicview4/dynamicview.qml</a></li>
<li><a href="qtquick-tutorials-dynamicview-dynamicview4-dynamicview4-qmlproject.html">tutorials/dynamicview/dynamicview4/dynamicview4.qmlproject</a></li>
</ul>
</div>
<!-- @@@tutorials/dynamicview/dynamicview4 -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-dynamicview-dynamicview3-example.html">QML 动态视图排序教程 3 - 移动拖动项</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
